// Copyright (c) 2000, <PIGS - Image Processing>
package save;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import javax.media.jai.PlanarImage;

import com.sun.media.jai.codec.ImageCodec;
import com.sun.media.jai.codec.ImageEncoder;
import com.sun.media.jai.codec.PNGEncodeParam;

public class SavePNG extends Save {

public boolean makeSave(PlanarImage src, String filename)
{
	    boolean flag=true;
		
		if (src.getNumBands()==3)
		{			
			try {
				OutputStream os = new FileOutputStream(filename);
				PNGEncodeParam param = new PNGEncodeParam.RGB();
				ImageEncoder enc = ImageCodec.createImageEncoder("PNG", os, param);
				enc.encode(src);
				os.close();

			} catch (IOException e) {
				e.printStackTrace();
				System.out.println("Error to save in PNG format");
				System.exit(0);
				flag=false;
			}
		}
		if (src.getNumBands()==1)
		{			
			try {
				OutputStream os = new FileOutputStream(filename);
				PNGEncodeParam param = new PNGEncodeParam.Gray();
				ImageEncoder enc = ImageCodec.createImageEncoder("PNG", os, param);
				enc.encode(src);
				os.close();

			} catch (IOException e) {
				e.printStackTrace();
				System.out.println("Error to save in PNG format");
				System.exit(0);
				flag=false;
			}
		}
		return flag;
}

}
